package cn.com.homeyoda.aimate.aihealth.utils;

import android.view.View;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * RxJava 实现防抖点击事件
 */
public class RxDebounceClickListener implements View.OnClickListener {
    private static final long DEBOUNCE_TIME = 500; // 防抖时间（毫秒）
    private Disposable disposable;

    @Override
    public void onClick(View v) {
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose(); // 取消之前的订阅
        }

        disposable = Observable.just(v)
                .throttleFirst(DEBOUNCE_TIME, java.util.concurrent.TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this::onValidClick);
    }

    protected void onValidClick(View view) {
        // 子类覆盖实现逻辑
    }
}
